﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;

public partial class Topics_TopImages : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            CMSEntities cms = new CMSEntities();
            var lst= cms.KeyValue.Where(x => x.Key == "TopicList_top_imgurl").ToList();
            if (lst.Count > 0)
            {
                var c = lst[0];
                txtUrl.Text = c.Value;
            }
            var lst2 = cms.KeyValue.Where(x => x.Key == "TopicList_top_img").ToList();
            if (lst2.Count > 0)
            {
                var c = lst2[0];
                Image1.ImageUrl = "~/file/topics/face/" + c.Value;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
     
        CMSEntities cms = new CMSEntities();
        if (cms.KeyValue.Any(x => x.Key == "TopicList_top_imgurl"))
        {
            var c = cms.KeyValue.First(x => x.Key == "TopicList_top_imgurl");
            c.Value = txtUrl.Text;
        }
        else
        {
            KeyValue c = new KeyValue();
            c.Key = "TopicList_top_imgurl";
            c.Value = txtUrl.Text;
            c.Desc = "专题首页图片下面的链接";
            cms.KeyValue.AddObject(c);
        }
        cms.SaveChanges();


        if (FileUpload1.HasFile)
        {
            string path = Server.MapPath("~/file/topics/face/");
            string file = Guid.NewGuid().ToString();
            string edx = FileUpload1.FileName.Substring(FileUpload1.FileName.IndexOf("."));
            string fileName = path + file + edx;
            FileUpload1.SaveAs(fileName);
            if (cms.KeyValue.Any(x => x.Key == "TopicList_top_img"))
            {
                var c = cms.KeyValue.First(x => x.Key == "TopicList_top_img");
                c.Value = file + edx;
            }
            else
            {
                KeyValue c = new KeyValue();
                c.Key = "TopicList_top_img";
                c.Value = file+edx;
                c.Desc = "专题首页图片下面的图片地址";
                cms.KeyValue.AddObject(c);
            }
            cms.SaveChanges();
            var lst2 = cms.KeyValue.Where(x => x.Key == "TopicList_top_img").ToList();
            if (lst2.Count > 0)
            {
                var c = lst2[0];
                Image1.ImageUrl = "~/file/topics/face/" + c.Value;
            }
        }
    }
}